import type { HVNController } from './definition'
import type { Worker } from '@/controller/base/types/worker'
import type { Remote } from '@/controller/base/types/remote'
import { useWorker, useRemote, wrapSafeActions } from '@/controller/base/index'
import { RemoteTypes } from '@/controller/base/remote'
import { createBaseState, BaseGetters, BaseActions, createBaseApplianceControl } from '../BaseAppliance'
import { defineStore } from 'pinia'
import {
  HVN_SETTING_NIGHT_MODE,
  HVN_SETTING_NIGHT_MODE_STARTHOUR,
  HVN_SETTING_NIGHT_MODE_STARTMIN,
  HVN_SETTING_NIGHT_MODE_ENDHOUR,
  HVN_SETTING_NIGHT_MODE_ENDMIN,
  HVN_SETTING_DA_VOLUME,
  HVN_SETTING_NIGHT_MODE_TIME,
} from '@/constants/Command'
import type { WS } from '@/utils/socket/types'

/**
 * 初始化数据
 */
export const createHVNState: () => HVNController.State = () => ({
  ...createBaseState(),
  nightMode: 0,
  nightMode_startHour: 0, //夜间模式开始小时
  nightMode_startMinute: 0, //夜间模式开始分钟
  nightMode_endHour: 0, //夜间模式终止小时
  nightMode_endMinute: 0, //夜间模式终止分钟
  volume: 0, //音量
  nightMode_timeSetting: 0, //夜间模式时间设置开关
  volume_min: 0, //音量最小值
  volume_max: 10, //音量最大值
})

export const HVNGetters: HVNController.Getters = {
  ...BaseGetters,
}
export const HVNActions: HVNController.Actions = wrapSafeActions<HVNController.Store, HVNController.Actions>({
  ...BaseActions,
})

/**
 * 组合API
 */
const useStore = defineStore<'HVN', HVNController.State, HVNController.Getters, HVNController.Actions>('HVN', {
  state: createHVNState,
  getters: HVNGetters,
  actions: HVNActions,
})

// 组件使用
export function useHVNControl(): HVNController.Control {
  const store = useStore() as HVNController.Store
  const worker = useWorker(store) as Worker.ApplianceWorker<HVNController.Store>
  const remote = useRemote(store) as Remote.ApplianceRemote<HVNController.Store>
  return { store, worker, remote }
}

// 页面使用
export function createHVNControl(control: HVNController.Control = useHVNControl()): HVNController.Control {
  const { store, worker, remote } = control

  createBaseApplianceControl(control, { remoteType: RemoteTypes.Oem })

  useHVNBinding(control)

  worker.onLoad(() => {
    return Promise.all([remote.loadAllSetting()])
  })

  return control
}

export function useHVNBinding(control: HVNController.Control) {
  const { worker } = control

  // Settings监听
  const syncMap: Partial<Record<keyof HVNController.State, string>> = {
    nightMode: HVN_SETTING_NIGHT_MODE,
    nightMode_startHour: HVN_SETTING_NIGHT_MODE_STARTHOUR,
    nightMode_startMinute: HVN_SETTING_NIGHT_MODE_STARTMIN,
    nightMode_endHour: HVN_SETTING_NIGHT_MODE_ENDHOUR,
    nightMode_endMinute: HVN_SETTING_NIGHT_MODE_ENDMIN,
    nightMode_timeSetting: HVN_SETTING_NIGHT_MODE_TIME,
  }
  Object.entries(syncMap).forEach((entries) => {
    const [prop, key] = entries as [keyof HVNController.State, string]
    worker.useGetValueBinding(prop, {
      key,
      from: 'setting',
    })
  })

  worker.useGetValueBinding('volume_min', {
    key: HVN_SETTING_DA_VOLUME,
    from: 'setting',
    patcher: (patchState, reference) => {
      if (reference.item as WS.MessageItem) {
        patchState.volume = reference.item.value as number
        patchState.volume_min = reference.item.min
        patchState.volume_max = reference.item.max
      }
    },
  })
}
